package com.tuling.netty.session;

import io.netty.channel.Channel;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Session {
    // Map<doctorId, Map<doctorIp, channel>>
    private Map<String, Map<String, Channel>> doctorMap = new ConcurrentHashMap<>();
    // Map<channelId, doctorChannel>
    private Map<String, DoctorChannel> sessionMap = new ConcurrentHashMap<>();

    private static Session session = new Session();

    public static Session getSession() {
        return session;
    }

    public void bind(String doctorId, String doctorIp, Channel channel) {
        String channelId = channel.id().asLongText();
        DoctorChannel doctorChannel = new DoctorChannel();
        doctorChannel.setDoctorId(doctorId);
        doctorChannel.setChannel(channel);

        sessionMap.put(channelId, doctorChannel);
        Map<String, Channel> doctorIpMap = doctorMap.get(doctorId);
        if (null == doctorIpMap || doctorIpMap.isEmpty()) {
            doctorIpMap = new ConcurrentHashMap<>();
        }
        doctorIpMap.put(doctorIp, channel);

        doctorMap.put(doctorId, doctorIpMap);
    }

    public void unbind(Channel channel) {
        String channelId = channel.id().asLongText();

        InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.remoteAddress();
        String doctorIp = inetSocketAddress.getAddress().getHostAddress();
        if (doctorIp.startsWith("//")) {
            doctorIp = doctorIp.substring(1);
        }

        DoctorChannel doctorChannel = sessionMap.get(channelId);
        if (null != doctorChannel) {
            String doctorId = doctorChannel.getDoctorId();
            // 删除当前ip的channel
            Map<String, Channel> doctorIpMap = doctorMap.get(doctorId);
            if(doctorIpMap != null) {
                doctorIpMap.remove(doctorIp);
                if (doctorIpMap.isEmpty()) {
                    doctorMap.remove(doctorId);
                }
            }
            sessionMap.remove(channelId);
        }
    }

    public List<Channel> getAll(){
        List<Channel> list = new ArrayList<>();
        for (DoctorChannel value : sessionMap.values()) {
            list.add(value.getChannel());
        }
        return list;
    }

    public List<Channel> getChannel(String doctorId, String doctorIp) {
        List<Channel> list = new ArrayList<>();

        Map<String, Channel> doctorIpMap = doctorMap.get(doctorId);
        if (null == doctorIpMap || doctorIpMap.isEmpty()) {
            return list;
        }

        // 如果doctorIp为空, 那么返回医生编号对应的所有客户端,
        // 即如果一个医生编号被多人使用, 审方时没有传入doctorIp,那么会把审方结果推送给所有使用医生账号登录的人
        // 2021-09-01 超哥要求
        if (null == doctorIp || "".equals(doctorIp)) {
            list = new ArrayList<>(doctorIpMap.values());
        } else {
            Channel channel = doctorIpMap.get(doctorIp);
            if (null != channel) {
                list.add(channel);
            }
        }

        return list;
    }
}